<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li class="active"><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 通过配置项启动数据库">1. 通过配置项启动数据库</a><ul><li><a href="#t11.1 mongo.conf">1.1 mongo.conf</a></li><li><a href="#t21.2 启动服务器">1.2 启动服务器</a></li><li><a href="#t31.3 启动客户端">1.3 启动客户端</a></li></ul></li><li><a href="#t42. 导入导出数据">2. 导入导出数据</a><ul><li><a href="#t52.1 准备数据">2.1 准备数据</a></li><li><a href="#t62.2 备份记录">2.2 备份记录</a></li><li><a href="#t72.3 删除记录">2.3 删除记录</a></li><li><a href="#t82.4 导入记录">2.4 导入记录</a></li></ul></li><li><a href="#t93. 备份与恢复">3. 备份与恢复</a><ul><li><a href="#t103.1 mongodump">3.1 mongodump</a></li><li><a href="#t113.2 mongorestore">3.2 mongorestore</a></li></ul></li><li><a href="#t124. 直接拷贝数据">4. 直接拷贝数据</a></li><li><a href="#t135. 锁定和解锁数据库">5. 锁定和解锁数据库</a></li><li><a href="#t146. 安全措施">6. 安全措施</a><ul><li><a href="#t156.1 用户管理">6.1 用户管理</a></li><li><a href="#t166.1.1 查看角色">6.1.1 查看角色</a></li><li><a href="#t176.1.2 老的创建用户的方法">6.1.2 老的创建用户的方法</a></li><li><a href="#t186.1.3 新的创建用户的方法">6.1.3 新的创建用户的方法</a></li><li><a href="#t196.1.4 查看用户的权限">6.1.4 查看用户的权限</a></li><li><a href="#t206.1.5 服务器启动权限认证">6.1.5 服务器启动权限认证</a></li><li><a href="#t216.1.6 用户登录和修改密码">6.1.6 用户登录和修改密码</a></li><li><a href="#t226.1.7 修改个人信息">6.1.7 修改个人信息</a></li></ul></li><li><a href="#t237. 数据库高级命令">7. 数据库高级命令</a><ul><li><a href="#t247.1 准备数据">7.1 准备数据</a></li><li><a href="#t257.2 count">7.2 count</a></li><li><a href="#t267.2 查找不重复的值 distinct">7.2 查找不重复的值 distinct</a></li><li><a href="#t277.3  group 分组">7.3  group 分组</a><ul><li><a href="#t287.3.1 按城市分组，求每个城市里符合条件的人的年龄总和">7.3.1 按城市分组，求每个城市里符合条件的人的年龄总和</a></li><li><a href="#t297.4 删除集合">7.4 删除集合</a></li><li><a href="#t307.5 runCommand常用命令">7.5 runCommand常用命令</a></li></ul></li></ul></li><li><a href="#t318. 什么固定集合">8. 什么固定集合</a><ul><li><a href="#t328.1 特性">8.1 特性</a></li><li><a href="#t338.2 创建固定集合">8.2 创建固定集合</a></li><li><a href="#t348.3 非固定集合转为固定集合">8.3 非固定集合转为固定集合</a></li></ul></li><li><a href="#t359. gridfs">9. gridfs</a><ul><li><a href="#t369.1  上传一个文件">9.1  上传一个文件</a></li><li><a href="#t379.2  获取并下载文件">9.2  获取并下载文件</a></li><li><a href="#t389.3  查看所有文件">9.3  查看所有文件</a></li><li><a href="#t399.4  删除文件">9.4  删除文件</a></li><li><a href="#t409.5  eval 服务器端脚本">9.5  eval 服务器端脚本</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x901A;&#x8FC7;&#x914D;&#x7F6E;&#x9879;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;">1. &#x901A;&#x8FC7;&#x914D;&#x7F6E;&#x9879;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93; <a href="#t01. &#x901A;&#x8FC7;&#x914D;&#x7F6E;&#x9879;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;"> # </a></h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x53C2;&#x6570;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">--dbpath</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x6570;&#x636E;&#x5E93;&#x6587;&#x4EF6;&#x7684;&#x76EE;&#x5F55;</td>
</tr>
<tr>
<td style="text-align:left">--port</td>
<td style="text-align:left">&#x7AEF;&#x53E3; &#x9ED8;&#x8BA4;&#x662F;27017 28017</td>
</tr>
<tr>
<td style="text-align:left">--fork</td>
<td style="text-align:left">&#x4EE5;&#x540E;&#x53F0;&#x5B88;&#x62A4;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x542F;&#x52A8;</td>
</tr>
<tr>
<td style="text-align:left">--logpath</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x65E5;&#x5FD7;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x8DEF;&#x5F84;</td>
</tr>
<tr>
<td style="text-align:left">--config</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</td>
</tr>
<tr>
<td style="text-align:left">--auth</td>
<td style="text-align:left">&#x4EE5;&#x5B89;&#x5168;&#x65B9;&#x5F0F;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E0D;&#x9A8C;&#x8BC1;</td>
</tr>
</tbody>
</table>
<h3 id="t11.1 mongo.conf">1.1 mongo.conf <a href="#t11.1 mongo.conf"> # </a></h3>
<pre><code class="lang-js">dbpath=E:\mongo\data
logpath=E:\mongo\log
port=<span class="hljs-number">50000</span>
</code></pre>
<h3 id="t21.2 &#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;">1.2 &#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668; <a href="#t21.2 &#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-js">mongod --config mongo.conf
</code></pre>
<h3 id="t31.3 &#x542F;&#x52A8;&#x5BA2;&#x6237;&#x7AEF;">1.3 &#x542F;&#x52A8;&#x5BA2;&#x6237;&#x7AEF; <a href="#t31.3 &#x542F;&#x52A8;&#x5BA2;&#x6237;&#x7AEF;"> # </a></h3>
<pre><code class="lang-js">mongo --port <span class="hljs-number">50000</span>
</code></pre>
<h2 id="t42. &#x5BFC;&#x5165;&#x5BFC;&#x51FA;&#x6570;&#x636E;">2. &#x5BFC;&#x5165;&#x5BFC;&#x51FA;&#x6570;&#x636E; <a href="#t42. &#x5BFC;&#x5165;&#x5BFC;&#x51FA;&#x6570;&#x636E;"> # </a></h2>
<p>&#x8FD9;&#x547D;&#x4EE4;&#x662F;&#x4FDD;&#x5B58;&#x6210;&#x4E86;&#x6587;&#x4EF6;&#x683C;&#x5F0F;</p>
<ul>
<li>mongoimport &#x5BFC;&#x51FA;&#x6570;&#x636E;</li>
<li>mongoexport &#x5BFC;&#x5165;&#x6570;&#x636E;</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">&#x53C2;&#x6570;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">-h [ --host ]</td>
<td style="text-align:left">&#x8FDE;&#x63A5;&#x7684;&#x6570;&#x636E;&#x5E93;</td>
</tr>
<tr>
<td style="text-align:left">--port</td>
<td style="text-align:left">&#x7AEF;&#x53E3;&#x53F7;</td>
</tr>
<tr>
<td style="text-align:left">-u</td>
<td style="text-align:left">&#x7528;&#x6237;&#x540D;</td>
</tr>
<tr>
<td style="text-align:left">-p</td>
<td style="text-align:left">&#x5BC6;&#x7801;</td>
</tr>
<tr>
<td style="text-align:left">-d</td>
<td style="text-align:left">&#x5BFC;&#x51FA;&#x7684;&#x6570;&#x636E;&#x5E93;</td>
</tr>
<tr>
<td style="text-align:left">-d</td>
<td style="text-align:left">&#x5BFC;&#x51FA;&#x7684;&#x6570;&#x636E;&#x5E93;</td>
</tr>
<tr>
<td style="text-align:left">-c</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x5BFC;&#x51FA;&#x7684;&#x96C6;&#x5408;</td>
</tr>
<tr>
<td style="text-align:left">-o</td>
<td style="text-align:left">&#x5BFC;&#x51FA;&#x7684;&#x6587;&#x4EF6;&#x5B58;&#x50A8;&#x8DEF;&#x5F84;</td>
</tr>
<tr>
<td style="text-align:left">-q</td>
<td style="text-align:left">&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;</td>
</tr>
</tbody>
</table>
<h3 id="t52.1 &#x51C6;&#x5907;&#x6570;&#x636E;">2.1 &#x51C6;&#x5907;&#x6570;&#x636E; <a href="#t52.1 &#x51C6;&#x5907;&#x6570;&#x636E;"> # </a></h3>
<pre><code class="lang-js">use school;
<span class="hljs-keyword">var</span> students = [];
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">10</span>;i++){
    students.push({<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>+i,<span class="hljs-attr">age</span>:i});
}
db.students.insert(students);
db.students.find();
</code></pre>
<h3 id="t62.2 &#x5907;&#x4EFD;&#x8BB0;&#x5F55;">2.2 &#x5907;&#x4EFD;&#x8BB0;&#x5F55; <a href="#t62.2 &#x5907;&#x4EFD;&#x8BB0;&#x5F55;"> # </a></h3>
<pre><code class="lang-js">mongoexport -h <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span> --port <span class="hljs-number">50000</span>  -d school -c students -o stu.bak
</code></pre>
<h3 id="t72.3 &#x5220;&#x9664;&#x8BB0;&#x5F55;">2.3 &#x5220;&#x9664;&#x8BB0;&#x5F55; <a href="#t72.3 &#x5220;&#x9664;&#x8BB0;&#x5F55;"> # </a></h3>
<pre><code class="lang-js">&gt; db.students.remove({});
WriteResult({ <span class="hljs-string">&quot;nRemoved&quot;</span> : <span class="hljs-number">10</span> })
</code></pre>
<h3 id="t82.4 &#x5BFC;&#x5165;&#x8BB0;&#x5F55;">2.4 &#x5BFC;&#x5165;&#x8BB0;&#x5F55; <a href="#t82.4 &#x5BFC;&#x5165;&#x8BB0;&#x5F55;"> # </a></h3>
<pre><code class="lang-js">mongoimport --port <span class="hljs-number">50000</span> --db school --collection students --file
stu.bak
</code></pre>
<h2 id="t93. &#x5907;&#x4EFD;&#x4E0E;&#x6062;&#x590D;">3. &#x5907;&#x4EFD;&#x4E0E;&#x6062;&#x590D; <a href="#t93. &#x5907;&#x4EFD;&#x4E0E;&#x6062;&#x590D;"> # </a></h2>
<h3 id="t103.1 mongodump">3.1 mongodump <a href="#t103.1 mongodump"> # </a></h3>
<p>&#x5728;Mongodb&#x4E2D;&#x6211;&#x4EEC;&#x4F7F;&#x7528;mongodump&#x547D;&#x4EE4;&#x6765;&#x5907;&#x4EFD;MongoDB&#x6570;&#x636E;&#x3002;&#x8BE5;&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x5BFC;&#x51FA;&#x6240;&#x6709;&#x6570;&#x636E;&#x5230;&#x6307;&#x5B9A;&#x76EE;&#x5F55;&#x4E2D;&#x3002;</p>
<blockquote>
<p>mongodump -h dbhost -d dbname -o dbdirectory</p>
<ul>
<li>-h MongDB&#x6240;&#x5728;&#x670D;&#x52A1;&#x5668;&#x5730;&#x5740;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;127.0.0.1&#xFF0C;&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x7AEF;&#x53E3;&#x53F7;&#xFF1A;127.0.0.1:27017</li>
<li>-d &#x9700;&#x8981;&#x5907;&#x4EFD;&#x7684;&#x6570;&#x636E;&#x5E93;&#x5B9E;&#x4F8B;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;test</li>
<li>-o &#x5907;&#x4EFD;&#x7684;&#x6570;&#x636E;&#x5B58;&#x653E;&#x4F4D;&#x7F6E;</li>
</ul>
</blockquote>
<pre><code class="lang-js">mongodump  -d school -o data.dmp
</code></pre>
<h3 id="t113.2 mongorestore">3.2 mongorestore <a href="#t113.2 mongorestore"> # </a></h3>
<p>mongodb&#x4F7F;&#x7528; mongorestore &#x547D;&#x4EE4;&#x6765;&#x6062;&#x590D;&#x5907;&#x4EFD;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<ul>
<li>--host MongoDB&#x6240;&#x5728;&#x670D;&#x52A1;&#x5668;&#x5730;&#x5740;</li>
<li>--db  -d  &#x9700;&#x8981;&#x6062;&#x590D;&#x7684;&#x6570;&#x636E;&#x5E93;&#x5B9E;&#x4F8B;</li>
<li><path> &#x6700;&#x540E;&#x7684;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8BBE;&#x7F6E;&#x5907;&#x4EFD;&#x6570;&#x636E;&#x6240;&#x5728;&#x4F4D;&#x7F6E;</path></li>
</ul>
<pre><code class="lang-js">mongorestore  data.dmp
mongorestore -d school data.bmp/school
</code></pre>
<blockquote>
<p>Mongodump&#x53EF;&#x4EE5;backup&#x6574;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x800C;mongoexport&#x8981;&#x5BF9;&#x6BCF;&#x4E2A;collection&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x6700;&#x4E3B;&#x8981;&#x7684;&#x533A;&#x522B;&#x4E5F;&#x662F;&#x9009;&#x62E9;&#x7684;&#x6807;&#x51C6;&#x662F;mongoexport&#x8F93;&#x51FA;&#x7684;JSON&#x6BD4;Mongodump&#x7684;BSON&#x53EF;&#x8BFB;&#x6027;&#x66F4;&#x9AD8;&#xFF0C;&#x8FDB;&#x800C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5BF9;JSON&#x6587;&#x4EF6;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x7136;&#x540E;&#x8FD8;&#x539F;&#x6570;&#x636E;&#xFF08;BSON&#x8F6C;&#x6362;JSON&#x5B58;&#x5728;&#x6F5C;&#x5728;&#x517C;&#x5BB9;&#x95EE;&#x9898;&#xFF09;&#x3002;</p>
</blockquote>
<h2 id="t124. &#x76F4;&#x63A5;&#x62F7;&#x8D1D;&#x6570;&#x636E;">4. &#x76F4;&#x63A5;&#x62F7;&#x8D1D;&#x6570;&#x636E; <a href="#t124. &#x76F4;&#x63A5;&#x62F7;&#x8D1D;&#x6570;&#x636E;"> # </a></h2>
<h2 id="t135. &#x9501;&#x5B9A;&#x548C;&#x89E3;&#x9501;&#x6570;&#x636E;&#x5E93;">5. &#x9501;&#x5B9A;&#x548C;&#x89E3;&#x9501;&#x6570;&#x636E;&#x5E93; <a href="#t135. &#x9501;&#x5B9A;&#x548C;&#x89E3;&#x9501;&#x6570;&#x636E;&#x5E93;"> # </a></h2>
<p>&#x4E3A;&#x4E86;&#x6570;&#x636E;&#x7684;&#x5B8C;&#x6574;&#x6027;&#x548C;&#x4E00;&#x81F4;&#x6027;&#xFF0C;&#x5BFC;&#x51FA;&#x524D;&#x8981;&#x5148;&#x9501;&#x5B9A;&#x5199;&#x5165;&#xFF0C;&#x5BFC;&#x51FA;&#x540E;&#x518D;&#x89E3;&#x9501;&#x3002;</p>
<pre><code class="lang-js">&gt; use admin;
switched to db admin
&gt; db.runCommand({<span class="hljs-attr">fsync</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">lock</span>:<span class="hljs-number">1</span>});
{
        <span class="hljs-string">&quot;info&quot;</span> : <span class="hljs-string">&quot;now locked against writes, use db.fsyncUnlock() to unlock&quot;</span>,
        <span class="hljs-string">&quot;seeAlso&quot;</span> : <span class="hljs-string">&quot;http://dochub.mongodb.org/core/fsynccommand&quot;</span>,
        <span class="hljs-string">&quot;ok&quot;</span> : <span class="hljs-number">1</span>
}
&gt; db.fsyncUnlock();
{ <span class="hljs-string">&quot;ok&quot;</span> : <span class="hljs-number">1</span>, <span class="hljs-string">&quot;info&quot;</span> : <span class="hljs-string">&quot;unlock completed&quot;</span> }
</code></pre>
<h2 id="t146. &#x5B89;&#x5168;&#x63AA;&#x65BD;">6. &#x5B89;&#x5168;&#x63AA;&#x65BD; <a href="#t146. &#x5B89;&#x5168;&#x63AA;&#x65BD;"> # </a></h2>
<ul>
<li>&#x7269;&#x7406;&#x9694;&#x79BB;</li>
<li>&#x7F51;&#x7EDC;&#x9694;&#x79BB;</li>
<li>&#x9632;&#x706B;&#x5899;(IP/IP&#x6BB5;/&#x767D;&#x540D;&#x5355;/&#x9ED1;&#x540D;&#x5355;)</li>
<li>&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x9A8C;&#x8BC1;</li>
</ul>
<h3 id="t156.1 &#x7528;&#x6237;&#x7BA1;&#x7406;">6.1 &#x7528;&#x6237;&#x7BA1;&#x7406; <a href="#t156.1 &#x7528;&#x6237;&#x7BA1;&#x7406;"> # </a></h3>
<h3 id="t166.1.1 &#x67E5;&#x770B;&#x89D2;&#x8272;">6.1.1 &#x67E5;&#x770B;&#x89D2;&#x8272; <a href="#t166.1.1 &#x67E5;&#x770B;&#x89D2;&#x8272;"> # </a></h3>
<pre><code class="lang-js">show roles;
</code></pre>
<p>&#x5185;&#x7F6E;&#x89D2;&#x8272;</p>
<ul>
<li>&#x6570;&#x636E;&#x5E93;&#x7528;&#x6237;&#x89D2;&#x8272;&#xFF1A;read&#x3001;readWrite&#xFF1B;</li>
<li>&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x89D2;&#x8272;&#xFF1A;dbAdmin&#x3001;dbOwner&#x3001;userAdmin;</li>
<li>&#x96C6;&#x7FA4;&#x7BA1;&#x7406;&#x89D2;&#x8272;&#xFF1A;clusterAdmin&#x3001;clusterManager&#x3001;clusterMonitor&#x3001;hostManage&#xFF1B;</li>
<li>&#x5907;&#x4EFD;&#x6062;&#x590D;&#x89D2;&#x8272;&#xFF1A;backup&#x3001;restore&#xFF1B;</li>
<li>&#x6240;&#x6709;&#x6570;&#x636E;&#x5E93;&#x89D2;&#x8272;&#xFF1A;readAnyDatabase&#x3001;readWriteAnyDatabase&#x3001;userAdminAnyDatabase&#x3001;dbAdminAnyDatabase</li>
<li>&#x8D85;&#x7EA7;&#x7528;&#x6237;&#x89D2;&#x8272;&#xFF1A;root</li>
<li>&#x5185;&#x90E8;&#x89D2;&#x8272;&#xFF1A;__system</li>
</ul>
<h3 id="t176.1.2 &#x8001;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5;">6.1.2 &#x8001;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5; <a href="#t176.1.2 &#x8001;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5;"> # </a></h3>
<pre><code class="lang-js">&gt; db.addUser(<span class="hljs-string">&apos;zfpx&apos;</span>,<span class="hljs-string">&apos;123456&apos;</span>);
WARNING: The <span class="hljs-string">&apos;addUser&apos;</span> shell helper is DEPRECATED. Please use <span class="hljs-string">&apos;createUser&apos;</span> inste
ad
Successfully added user: { <span class="hljs-string">&quot;user&quot;</span> : <span class="hljs-string">&quot;zfpx&quot;</span>, <span class="hljs-string">&quot;roles&quot;</span> : [ <span class="hljs-string">&quot;root&quot;</span> ] }
show roles;
</code></pre>
<h3 id="t186.1.3 &#x65B0;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5;">6.1.3 &#x65B0;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5; <a href="#t186.1.3 &#x65B0;&#x7684;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7684;&#x65B9;&#x6CD5;"> # </a></h3>
<pre><code class="lang-js">db.createUser({
    <span class="hljs-attr">user</span>:<span class="hljs-string">&quot;zfpx2&quot;</span>,
    <span class="hljs-attr">pwd</span>:<span class="hljs-string">&quot;123456&quot;</span>,
    <span class="hljs-attr">roles</span>:[
        {
            <span class="hljs-attr">role</span>:<span class="hljs-string">&quot;readWrite&quot;</span>,
            <span class="hljs-attr">db</span>:<span class="hljs-string">&quot;school&quot;</span>
        },
        <span class="hljs-string">&apos;read&apos;</span>
   ]
})

</code></pre>
<pre><code class="lang-js">&gt; db.createUser({<span class="hljs-attr">user</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>,<span class="hljs-attr">pwd</span>:<span class="hljs-string">&apos;123456&apos;</span>,<span class="hljs-attr">roles</span>:[{<span class="hljs-attr">role</span>:<span class="hljs-string">&apos;read&apos;</span>,<span class="hljs-attr">db</span>:<span class="hljs-string">&apos;school&apos;</span>}]});
Successfully added user: {
        <span class="hljs-string">&quot;user&quot;</span> : <span class="hljs-string">&quot;zfpx2&quot;</span>,
        <span class="hljs-string">&quot;roles&quot;</span> : [
                {
                        <span class="hljs-string">&quot;role&quot;</span> : <span class="hljs-string">&quot;read&quot;</span>,
                        <span class="hljs-string">&quot;db&quot;</span> : <span class="hljs-string">&quot;school&quot;</span>
                }
        ]
}
</code></pre>
<h3 id="t196.1.4 &#x67E5;&#x770B;&#x7528;&#x6237;&#x7684;&#x6743;&#x9650;">6.1.4 &#x67E5;&#x770B;&#x7528;&#x6237;&#x7684;&#x6743;&#x9650; <a href="#t196.1.4 &#x67E5;&#x770B;&#x7528;&#x6237;&#x7684;&#x6743;&#x9650;"> # </a></h3>
<pre><code class="lang-js">&gt; db.runCommand({<span class="hljs-attr">usersInfo</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>,<span class="hljs-attr">showPrivileges</span>:<span class="hljs-literal">true</span>});
{
        <span class="hljs-string">&quot;users&quot;</span> : [
                {
                        <span class="hljs-string">&quot;_id&quot;</span> : <span class="hljs-string">&quot;admin.zfpx2&quot;</span>,
                        <span class="hljs-string">&quot;user&quot;</span> : <span class="hljs-string">&quot;zfpx2&quot;</span>,
                        <span class="hljs-string">&quot;db&quot;</span> : <span class="hljs-string">&quot;admin&quot;</span>,
                        <span class="hljs-string">&quot;roles&quot;</span> : [
                                {
                                        <span class="hljs-string">&quot;role&quot;</span> : <span class="hljs-string">&quot;read&quot;</span>,
                                        <span class="hljs-string">&quot;db&quot;</span> : <span class="hljs-string">&quot;school&quot;</span>
                                }
                ]
}
</code></pre>
<h3 id="t206.1.5 &#x670D;&#x52A1;&#x5668;&#x542F;&#x52A8;&#x6743;&#x9650;&#x8BA4;&#x8BC1;">6.1.5 &#x670D;&#x52A1;&#x5668;&#x542F;&#x52A8;&#x6743;&#x9650;&#x8BA4;&#x8BC1; <a href="#t206.1.5 &#x670D;&#x52A1;&#x5668;&#x542F;&#x52A8;&#x6743;&#x9650;&#x8BA4;&#x8BC1;"> # </a></h3>
<pre><code class="lang-js">dbpath=E:\mongo\data
logpath=E:\mongo\log
port=<span class="hljs-number">50000</span>
auth=<span class="hljs-literal">true</span>
</code></pre>
<h3 id="t216.1.6 &#x7528;&#x6237;&#x767B;&#x5F55;&#x548C;&#x4FEE;&#x6539;&#x5BC6;&#x7801;">6.1.6 &#x7528;&#x6237;&#x767B;&#x5F55;&#x548C;&#x4FEE;&#x6539;&#x5BC6;&#x7801; <a href="#t216.1.6 &#x7528;&#x6237;&#x767B;&#x5F55;&#x548C;&#x4FEE;&#x6539;&#x5BC6;&#x7801;"> # </a></h3>
<pre><code>&gt; use admin;
&gt; use admin;
switched to db admin
&gt; db.auth(&apos;zfpx&apos;,&apos;123456&apos;)
1
&gt; db.changeUserPassword(&apos;zfpx&apos;,&apos;123&apos;);
&gt; db.auth(&apos;zfpx&apos;,&apos;123&apos;)
1
</code></pre><h3 id="t226.1.7 &#x4FEE;&#x6539;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;">6.1.7 &#x4FEE;&#x6539;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F; <a href="#t226.1.7 &#x4FEE;&#x6539;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;"> # </a></h3>
<pre><code class="lang-js">db.runCommand({<span class="hljs-attr">updateUser</span>:<span class="hljs-string">&apos;zfpx&apos;</span>,<span class="hljs-attr">pwd</span>:<span class="hljs-string">&apos;123&apos;</span>, <span class="hljs-attr">customData</span>:{
        <span class="hljs-attr">name</span>:<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&#x57F9;&#x8BAD;&apos;</span>,
        <span class="hljs-attr">email</span>:<span class="hljs-string">&apos;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e14081e162e5f5c58400d0103">[email&#160;protected]</a>&apos;</span>,
        <span class="hljs-attr">age</span>:<span class="hljs-number">18</span>,
}});
&gt; db.runCommand({<span class="hljs-attr">usersInfo</span>:<span class="hljs-string">&apos;zfpx&apos;</span>,<span class="hljs-attr">showPrivileges</span>:<span class="hljs-literal">true</span>})
</code></pre>
<ul>
<li>&#x7528;&#x6237;&#x7684;&#x64CD;&#x4F5C;&#x90FD;&#x9700;&#x8981;&#x5728;admin&#x6570;&#x636E;&#x5E93;&#x4E0B;&#x9762;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;</li>
<li>&#x5982;&#x679C;&#x5728;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x4E0B;&#x9762;&#x6267;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x5BF9;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;&#x751F;&#x6548;</li>
<li>addUser&#x5DF2;&#x7ECF;&#x5E9F;&#x5F03;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F1A;&#x521B;&#x5EFA;root&#x7528;&#x6237;&#xFF0C;&#x4E0D;&#x5B89;&#x5168;&#xFF0C;&#x4E0D;&#x518D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;</li>
</ul>
<h2 id="t237. &#x6570;&#x636E;&#x5E93;&#x9AD8;&#x7EA7;&#x547D;&#x4EE4;">7. &#x6570;&#x636E;&#x5E93;&#x9AD8;&#x7EA7;&#x547D;&#x4EE4; <a href="#t237. &#x6570;&#x636E;&#x5E93;&#x9AD8;&#x7EA7;&#x547D;&#x4EE4;"> # </a></h2>
<h3 id="t247.1 &#x51C6;&#x5907;&#x6570;&#x636E;">7.1 &#x51C6;&#x5907;&#x6570;&#x636E; <a href="#t247.1 &#x51C6;&#x5907;&#x6570;&#x636E;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> students = [
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">1</span>},
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">2</span>},
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx3&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">3</span>},
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx4&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5E7F;&#x4E1C;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">1</span>},
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx5&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5E7F;&#x4E1C;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">2</span>},
        {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx6&apos;</span>,<span class="hljs-attr">home</span>:<span class="hljs-string">&apos;&#x5E7F;&#x4E1C;&apos;</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">3</span>}
]
db.students.insert(students);
</code></pre>
<h3 id="t257.2 count">7.2 count <a href="#t257.2 count"> # </a></h3>
<p>&#x67E5;&#x770B;&#x8BB0;&#x5F55;&#x6570;</p>
<pre><code class="lang-js">db.students.find().count();
</code></pre>
<h3 id="t267.2 &#x67E5;&#x627E;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x503C; distinct">7.2 &#x67E5;&#x627E;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x503C; distinct <a href="#t267.2 &#x67E5;&#x627E;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x503C; distinct"> # </a></h3>
<pre><code class="lang-js">db.runCommand({<span class="hljs-attr">distinct</span>:<span class="hljs-string">&apos;students&apos;</span>,<span class="hljs-attr">key</span>:<span class="hljs-string">&apos;home&apos;</span>}).values;

[ <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>, <span class="hljs-string">&quot;&#x5E7F;&#x4E1C;&quot;</span> ]
</code></pre>
<h3 id="t277.3  group &#x5206;&#x7EC4;">7.3  group &#x5206;&#x7EC4; <a href="#t277.3  group &#x5206;&#x7EC4;"> # </a></h3>
<pre><code class="lang-js">db.runCommand({
        <span class="hljs-attr">group</span>:{
                <span class="hljs-attr">ns</span>:&#x96C6;&#x5408;&#x540D;&#x79F0;&#xFF0C;
                key:&#x5206;&#x7EC4;&#x7684;&#x952E;,
                <span class="hljs-attr">initial</span>:&#x521D;&#x59CB;&#x503C;,
                <span class="hljs-attr">$reduce</span>:&#x5206;&#x89E3;&#x5668;
                condition:&#x6761;&#x4EF6;,
                <span class="hljs-attr">finalize</span>:&#x5B8C;&#x6210;&#x65F6;&#x7684;&#x5904;&#x7406;&#x5668;
        }
});
</code></pre>
<h4 id="t287.3.1 &#x6309;&#x57CE;&#x5E02;&#x5206;&#x7EC4;&#xFF0C;&#x6C42;&#x6BCF;&#x4E2A;&#x57CE;&#x5E02;&#x91CC;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x4EBA;&#x7684;&#x5E74;&#x9F84;&#x603B;&#x548C;">7.3.1 &#x6309;&#x57CE;&#x5E02;&#x5206;&#x7EC4;&#xFF0C;&#x6C42;&#x6BCF;&#x4E2A;&#x57CE;&#x5E02;&#x91CC;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x4EBA;&#x7684;&#x5E74;&#x9F84;&#x603B;&#x548C; <a href="#t287.3.1 &#x6309;&#x57CE;&#x5E02;&#x5206;&#x7EC4;&#xFF0C;&#x6C42;&#x6BCF;&#x4E2A;&#x57CE;&#x5E02;&#x91CC;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x4EBA;&#x7684;&#x5E74;&#x9F84;&#x603B;&#x548C;"> # </a></h4>
<pre><code class="lang-js">db.runCommand({
        <span class="hljs-attr">group</span>:{
                <span class="hljs-attr">ns</span>:<span class="hljs-string">&apos;students&apos;</span>,
                <span class="hljs-attr">key</span>:{<span class="hljs-attr">home</span>:<span class="hljs-literal">true</span>},
                <span class="hljs-attr">initial</span>:{<span class="hljs-attr">total</span>:<span class="hljs-number">0</span>},
                <span class="hljs-attr">$reduce</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">doc,result</span>)</span>{
                      result.total += doc.age;   
                },
                <span class="hljs-attr">condition</span>:{<span class="hljs-attr">age</span>:{<span class="hljs-attr">$gt</span>:<span class="hljs-number">1</span>}},
                <span class="hljs-attr">finalize</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">result</span>)</span>{
                    result.desc = <span class="hljs-string">&apos;&#x672C;&#x57CE;&#x5E02;&#x7684;&#x603B;&#x5E74;&#x9F84;&#x4E3A;&apos;</span>+result.total;
                }
        }
});
</code></pre>
<h4 id="t297.4 &#x5220;&#x9664;&#x96C6;&#x5408;">7.4 &#x5220;&#x9664;&#x96C6;&#x5408; <a href="#t297.4 &#x5220;&#x9664;&#x96C6;&#x5408;"> # </a></h4>
<pre><code class="lang-js">db.runCommand({<span class="hljs-attr">drop</span>:<span class="hljs-string">&apos;students&apos;</span>});
</code></pre>
<h4 id="t307.5 runCommand&#x5E38;&#x7528;&#x547D;&#x4EE4;">7.5 runCommand&#x5E38;&#x7528;&#x547D;&#x4EE4; <a href="#t307.5 runCommand&#x5E38;&#x7528;&#x547D;&#x4EE4;"> # </a></h4>
<pre><code class="lang-js">db.runCommand({<span class="hljs-attr">buildInfo</span>:<span class="hljs-number">1</span>});
db.runCommand({<span class="hljs-attr">getLastError</span>:<span class="hljs-string">&quot;students&quot;</span>});
db.persons.insert({<span class="hljs-attr">_id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-number">1</span>});
db.persons.insert({<span class="hljs-attr">_id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-number">1</span>});
db.runCommand({<span class="hljs-attr">getLastError</span>:<span class="hljs-string">&quot;students&quot;</span>});
</code></pre>
<h2 id="t318. &#x4EC0;&#x4E48;&#x56FA;&#x5B9A;&#x96C6;&#x5408;">8. &#x4EC0;&#x4E48;&#x56FA;&#x5B9A;&#x96C6;&#x5408; <a href="#t318. &#x4EC0;&#x4E48;&#x56FA;&#x5B9A;&#x96C6;&#x5408;"> # </a></h2>
<p>MongoDB &#x56FA;&#x5B9A;&#x96C6;&#x5408;&#xFF08;Capped Collections&#xFF09;&#x662F;&#x6027;&#x80FD;&#x51FA;&#x8272;&#x4E14;&#x6709;&#x7740;&#x56FA;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x5BF9;&#x4E8E;&#x5927;&#x5C0F;&#x56FA;&#x5B9A;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x60F3;&#x8C61;&#x5176;&#x5C31;&#x50CF;&#x4E00;&#x4E2A;&#x73AF;&#x5F62;&#x961F;&#x5217;&#xFF0C;&#x5F53;&#x96C6;&#x5408;&#x7A7A;&#x95F4;&#x7528;&#x5B8C;&#x540E;&#xFF0C;&#x518D;&#x63D2;&#x5165;&#x7684;&#x5143;&#x7D20;&#x5C31;&#x4F1A;&#x8986;&#x76D6;&#x6700;&#x521D;&#x59CB;&#x7684;&#x5934;&#x90E8;&#x7684;&#x5143;&#x7D20;&#xFF01;
<img src="http://img.zhufengpeixun.cn/firstinfirstout.png" alt="firstinfirstout"></p>
<h3 id="t328.1 &#x7279;&#x6027;">8.1 &#x7279;&#x6027; <a href="#t328.1 &#x7279;&#x6027;"> # </a></h3>
<ul>
<li><ol>
<li>&#x6CA1;&#x6709;&#x7D22;&#x5F15;</li>
</ol>
</li>
<li><ol start="2">
<li>&#x63D2;&#x5165;&#x548C;&#x67E5;&#x8BE2;&#x901F;&#x5EA6;&#x901F;&#x5EA6;&#x975E;&#x5E38;&#x5FEB; &#x4E0D;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x5206;&#x914D;&#x7A7A;&#x95F4;</li>
</ol>
</li>
<li><ol start="3">
<li>&#x7279;&#x522B;&#x9002;&#x5408;&#x5B58;&#x50A8;&#x65E5;&#x5FD7;</li>
</ol>
</li>
</ul>
<h3 id="t338.2 &#x521B;&#x5EFA;&#x56FA;&#x5B9A;&#x96C6;&#x5408;">8.2 &#x521B;&#x5EFA;&#x56FA;&#x5B9A;&#x96C6;&#x5408; <a href="#t338.2 &#x521B;&#x5EFA;&#x56FA;&#x5B9A;&#x96C6;&#x5408;"> # </a></h3>
<ul>
<li>&#x6211;&#x4EEC;&#x901A;&#x8FC7;createCollection&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x56FA;&#x5B9A;&#x96C6;&#x5408;&#xFF0C;&#x4E14;capped&#x9009;&#x9879;&#x8BBE;&#x7F6E;&#x4E3A;true&#xFF1A;</li>
<li>&#x8FD8;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x6587;&#x6863;&#x4E2A;&#x6570;,&#x52A0;&#x4E0A;max:1000&#x5C5E;&#x6027;&#xFF1A;</li>
<li>&#x5224;&#x65AD;&#x96C6;&#x5408;&#x662F;&#x5426;&#x4E3A;&#x56FA;&#x5B9A;&#x96C6;&#x5408;: db.logs.isCapped()</li>
<li>size &#x662F;&#x6574;&#x4E2A;&#x96C6;&#x5408;&#x7A7A;&#x95F4;&#x5927;&#x5C0F;&#xFF0C;&#x5355;&#x4F4D;&#x4E3A;&#x3010;KB&#x3011;</li>
<li>max &#x662F;&#x96C6;&#x5408;&#x6587;&#x6863;&#x4E2A;&#x6570;&#x4E0A;&#x7EBF;&#xFF0C;&#x5355;&#x4F4D;&#x662F;&#x3010;&#x4E2A;&#x3011;</li>
<li>&#x5982;&#x679C;&#x7A7A;&#x95F4;&#x5927;&#x5C0F;&#x5230;&#x8FBE;&#x4E0A;&#x9650;&#xFF0C;&#x5219;&#x63D2;&#x5165;&#x4E0B;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x65F6;&#xFF0C;&#x4F1A;&#x8986;&#x76D6;&#x7B2C;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#xFF1B;&#x5982;&#x679C;&#x6587;&#x6863;&#x4E2A;&#x6570;&#x5230;&#x8FBE;&#x4E0A;&#x9650;&#xFF0C;&#x540C;&#x6837;&#x63D2;&#x5165;&#x4E0B;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x65F6;&#xFF0C;&#x4F1A;&#x8986;&#x76D6;&#x7B2C;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x3002;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x4E0A;&#x9650;&#x5224;&#x65AD;&#x53D6;&#x7684;&#x662F;&#x3010;&#x4E0E;&#x3011;&#x7684;&#x903B;&#x8F91;&#x3002;</li>
<li>capped &#x5C01;&#x9876;&#x7684;</li>
</ul>
<pre><code class="lang-js"> db.createCollection(<span class="hljs-string">&apos;logs&apos;</span>,{<span class="hljs-attr">size</span>:<span class="hljs-number">50</span>,<span class="hljs-attr">max</span>:<span class="hljs-number">5</span>,<span class="hljs-attr">capped</span>:<span class="hljs-literal">true</span>});
</code></pre>
<h3 id="t348.3 &#x975E;&#x56FA;&#x5B9A;&#x96C6;&#x5408;&#x8F6C;&#x4E3A;&#x56FA;&#x5B9A;&#x96C6;&#x5408;">8.3 &#x975E;&#x56FA;&#x5B9A;&#x96C6;&#x5408;&#x8F6C;&#x4E3A;&#x56FA;&#x5B9A;&#x96C6;&#x5408; <a href="#t348.3 &#x975E;&#x56FA;&#x5B9A;&#x96C6;&#x5408;&#x8F6C;&#x4E3A;&#x56FA;&#x5B9A;&#x96C6;&#x5408;"> # </a></h3>
<pre><code class="lang-js">db.runCommand({<span class="hljs-attr">convertToCapped</span>:<span class="hljs-string">&quot;logs&quot;</span>,<span class="hljs-attr">size</span>:<span class="hljs-number">5</span>});
</code></pre>
<h2 id="t359. gridfs">9. gridfs <a href="#t359. gridfs"> # </a></h2>
<ul>
<li>gridfs&#x662F;mongodb&#x81EA;&#x5E26;&#x7684;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#xFF0C;&#x4F7F;&#x7528;&#x4E8C;&#x8FDB;&#x5236;&#x5B58;&#x50A8;&#x6587;&#x4EF6;&#x3002;</li>
<li>mongodb&#x53EF;&#x4EE5;&#x4EE5;BSON&#x683C;&#x5F0F;&#x4FDD;&#x5B58;&#x4E8C;&#x8FDB;&#x5236;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x4F46;&#x662F;BSON&#x5BF9;&#x8C61;&#x7684;&#x4F53;&#x79EF;&#x4E0D;&#x80FD;&#x8D85;&#x8FC7;4M&#x3002;&#x6240;&#x4EE5;mongodb&#x63D0;&#x4F9B;&#x4E86;<code>mongofiles</code>&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x5927;&#x6587;&#x4EF6;&#x900F;&#x660E;&#x5730;&#x5206;&#x5272;&#x6210;&#x5C0F;&#x6587;&#x4EF6;&#xFF08;256K&#xFF09;&#xFF0C;&#x4ECE;&#x800C;&#x4FDD;&#x5B58;&#x5927;&#x4F53;&#x79EF;&#x7684;&#x6570;&#x636E;&#x3002;</li>
<li>GridFS &#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x548C;&#x6062;&#x590D;&#x90A3;&#x4E9B;&#x8D85;&#x8FC7;16M&#xFF08;BSON&#x6587;&#x4EF6;&#x9650;&#x5236;&#xFF09;&#x7684;&#x6587;&#x4EF6;(&#x5982;&#xFF1A;&#x56FE;&#x7247;&#x3001;&#x97F3;&#x9891;&#x3001;&#x89C6;&#x9891;&#x7B49;)&#x3002;</li>
<li>GridFS &#x7528;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x6765;&#x5B58;&#x50A8;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF1A;fs.files&#x4E0E;fs.chunks&#x3002;</li>
<li>&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x5B9E;&#x9645;&#x5185;&#x5BB9;&#x88AB;&#x5B58;&#x5728;chunks(&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;)&#x4E2D;,&#x548C;&#x6587;&#x4EF6;&#x6709;&#x5173;&#x7684;meta&#x6570;&#x636E;(filename,content_type,&#x8FD8;&#x6709;&#x7528;&#x6237;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;)&#x5C06;&#x4F1A;&#x88AB;&#x5B58;&#x5728;files&#x96C6;&#x5408;&#x4E2D;&#x3002;</li>
<li><h3 id="t369.1  &#x4E0A;&#x4F20;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;">9.1  &#x4E0A;&#x4F20;&#x4E00;&#x4E2A;&#x6587;&#x4EF6; <a href="#t369.1  &#x4E0A;&#x4F20;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;"> # </a></h3>
</li>
<li>-d &#x6570;&#x636E;&#x5E93;&#x7684;&#x540D;&#x79F0;</li>
<li>-l &#x6E90;&#x6587;&#x4EF6;&#x7684;&#x4F4D;&#x7F6E;</li>
<li>put  &#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x540D;</li>
</ul>
<pre><code class="lang-js">mongofiles -d myfiles put test.txt
</code></pre>
<h3 id="t379.2  &#x83B7;&#x53D6;&#x5E76;&#x4E0B;&#x8F7D;&#x6587;&#x4EF6;">9.2  &#x83B7;&#x53D6;&#x5E76;&#x4E0B;&#x8F7D;&#x6587;&#x4EF6; <a href="#t379.2  &#x83B7;&#x53D6;&#x5E76;&#x4E0B;&#x8F7D;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">mongofiles -d myfiles  get <span class="hljs-string">&apos;test.txt&apos;</span>
</code></pre>
<h3 id="t389.3  &#x67E5;&#x770B;&#x6240;&#x6709;&#x6587;&#x4EF6;">9.3  &#x67E5;&#x770B;&#x6240;&#x6709;&#x6587;&#x4EF6; <a href="#t389.3  &#x67E5;&#x770B;&#x6240;&#x6709;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">mongofiles -d myfiles  list
&gt;db.fs.files.find()
&gt;db.fs.chunks.find({<span class="hljs-attr">files_id</span>:ObjectId(<span class="hljs-string">&apos;&apos;</span>)})
</code></pre>
<h3 id="t399.4  &#x5220;&#x9664;&#x6587;&#x4EF6;">9.4  &#x5220;&#x9664;&#x6587;&#x4EF6; <a href="#t399.4  &#x5220;&#x9664;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">mongofiles -d myfiles <span class="hljs-keyword">delete</span> <span class="hljs-string">&quot;test.txt&quot;</span>
</code></pre>
<h3 id="t409.5  eval &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;">9.5  eval &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C; <a href="#t409.5  eval &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;"> # </a></h3>
<ul>
<li>&#x6267;&#x884C;JS&#x8BED;&#x53E5;</li>
<li>&#x5B9A;&#x4E49;JS&#x5168;&#x5C40;&#x53D8;&#x91CF;</li>
<li>&#x5B9A;&#x4E49;&#x51FD;&#x6570;</li>
<li>Stored JavaScript</li>
</ul>
<pre><code class="lang-js">db.eval(<span class="hljs-string">&quot;1+1&quot;</span>);
db.eval(<span class="hljs-string">&quot;return &apos;hello&apos;&quot;</span>);
db.system.js.insert({<span class="hljs-attr">_id</span>:<span class="hljs-string">&quot;x&quot;</span>,<span class="hljs-attr">value</span>:<span class="hljs-number">1</span>});
db.eval(<span class="hljs-string">&quot;return x&quot;</span>);
db.system.js.insert({<span class="hljs-attr">_id</span>:<span class="hljs-string">&quot;say&quot;</span>,<span class="hljs-attr">value</span>:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{<span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello&apos;</span>}});
db.eval(<span class="hljs-string">&quot;say()&quot;</span>);
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
